﻿using ICAM.DataLayer;
using ICAM.Model;
using ICAM.View;
using ICAM.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ICAM
{
    public class MainViewModule : ViewModelBase
    {
        private User userInfo;

        public User UserInfo
        {
            get
            {
                return userInfo;
            }

            set
            {
                userInfo = value;
                this.OnPropertyChanged("UserInfo");
            }
        }
        public ObservableCollection<TopAlarmStatistics> AlaramInfoColletion { get; set; }
        public ObservableCollection<TopAlarmStatistics> AlaramAddressCollection { get; set; }
        public ObservableCollection<AlarmItemViewModule> AlaramCollection { get; set; }
        public List<AlarmStatistics> AlaramStatic { get; set; }

        private Visibility _ShowAlaramDetailVisibility = Visibility.Collapsed;
        public Visibility ShowAlaramDetailVisibility
        {
            get { return _ShowAlaramDetailVisibility; }

            set
            {
                _ShowAlaramDetailVisibility = value;
                this.OnPropertyChanged("ShowAlaramDetailVisibility");
            }
        }
        private Visibility _ShowAlaramVisibility = Visibility.Visible;
        public Visibility ShowAlaramVisibility
        {
            get { return _ShowAlaramVisibility; }

            set
            {
                _ShowAlaramVisibility = value;
                this.OnPropertyChanged("ShowAlaramVisibility");
            }
        }

        private string alaramMessage;
        public string AlaramMessage
        {
            get { return alaramMessage; }
            set
            {
                alaramMessage = value;
                OnPropertyChanged("AlaramMessage");
            }
        }

        public ICommand ShowAlaramDetailCommand { get; set; }
        public ICommand ShowAlaramListCommand { get; set; }
        public ICommand ShowLogCommand { get; set; }
        public ICommand ExitCommand { get; set; }

        public ICommand AlaramMessageCommand { get; set; }

        private Thread thread;
        private Random numRandom;
        public MainViewModule()
        {
            numRandom = new Random(1);
            var data = new DataService();

            this.userInfo = data.GetLoginUser();

            AlaramCollection = new ObservableCollection<AlarmItemViewModule>();

            var alaramList = data.GetAlarmInfo(4);

            foreach (var item in alaramList)
            {
                data.GetAlarmRestorationInfo(item, numRandom.Next(15));
                AlaramCollection.Add(new AlarmItemViewModule(item, this.userInfo));
            }

            var list = data.GetAlarmDistribution();
            var listLoc = data.GetAlarmLocation();
            AlaramInfoColletion = new ObservableCollection<TopAlarmStatistics>();
            AlaramAddressCollection = new ObservableCollection<TopAlarmStatistics>();

            foreach (var item in list)
            {
                AlaramInfoColletion.Add(item);
            }
            foreach (var item in listLoc)
            {
                AlaramAddressCollection.Add(item);
            }

            AlaramStatic = data.GetAlarmStatistics();

            ShowAlaramDetailCommand = new DelegateCommand(ExecuteAlaramDetailCommand);
            ShowAlaramListCommand = new DelegateCommand(ExecuteAlaramListCommand);
            ShowLogCommand = new DelegateCommand(ExecuteShowLogCommand);
            ExitCommand = new DelegateCommand(ExecuteExitCommand);
            AlaramMessageCommand = new DelegateCommand(ExecuteAlaramMessageCommand);

            this.thread = new Thread(TestData);
            this.thread.IsBackground = true;
            this.thread.Start();
        }

        private void TestData()
        {
            //收到3条告警消息，点击查看
            int num = 1;
            while (true)
            {
                int index = 0;
                while (index++ < 100)
                {
                    Thread.Sleep(100);
                }
                var data = new DataService();
                var list = data.GetAlarmInfo(num);
                Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
                {
                    if (list.Count > 0)
                    {
                        AlaramMessage = $"收到{list.Count}条告警消息，点击查看";
                    }
                    foreach (var item in list)
                    {
                        data.GetAlarmRestorationInfo(item, numRandom.Next(15));
                        AlaramCollection.Insert(0, new AlarmItemViewModule(item, this.userInfo));
                    }
                }));
                num++;
                if (num >= 3)
                {
                    num = 1;
                }
            }
        }

        // 查看告警消息
        private void ExecuteAlaramMessageCommand(object param)
        {
            AlaramMessage = "";
        }

        // 查看全部告警
        private void ExecuteAlaramDetailCommand(object param)
        {
            ShowAlaramDetailVisibility = Visibility.Visible;
            ShowAlaramVisibility = Visibility.Collapsed;
        }
        // 返回主界面
        private void ExecuteAlaramListCommand(object param)
        {
            ShowAlaramDetailVisibility = Visibility.Collapsed;
            ShowAlaramVisibility = Visibility.Visible;
        }
        // 查看日志
        private void ExecuteShowLogCommand(object param)
        {
            Log log = new Log();
            log.Owner = Application.Current.MainWindow;
            log.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            log.ShowDialog();
        }

        // 退出
        private void ExecuteExitCommand(object param)
        {
            Exit(true);
        }

        public bool Exit(bool isClosed = false)
        {
            bool isCancel = true;
            ExitView view = new ExitView();
            view.DataContext = this;
            view.Owner = Application.Current.MainWindow;
            view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (view.ShowDialog() ?? false)
            {
                if (isClosed)
                {
                    Environment.Exit(0);
                }
                isCancel = false;
            }
            return isCancel;
        }
    }
}
